#!/usr/bin/env python2
# -*- coding: utf-8 -*-
"""
Created on Sun Jan 14 21:31:27 2018

@author: dve
"""

import csv


num_sims = 10000
for i in [1,5,10,20,50,100,"heritable"]:
    rel_fit = []
    fix = []
    pop_fixed = {}
    
    print i
    filename = "./data_newmut_regress2/"+ str(i) +"/iftype2.oevo"
    with open(filename, 'rb') as csvfile:
        extreader = csv.reader(csvfile, delimiter=',')
        for row in extreader:
            pop_fixed[int(row[0])] = int(row[1])

    for j in xrange(1, num_sims+1):
        filename = "./data_newmut_regress2/" + str(i) + "/sim_num_" + str(j) + "_new_mutant_1.oevo"
        with open(filename, 'rb') as csvfile:
            distreader = csv.reader(csvfile, delimiter=',')
            for row in distreader:
                rel_fit_val = 100*float(row[2])/float(row[3])
                rel_fit.append(rel_fit_val)
                fix.append(0)

            fix[-1] = pop_fixed[j]

    file_to_write = "./new_mut_processed/processed_"+str(i)+".csv"
    if len(rel_fit) != len(fix):
        print "length error"
    with open(file_to_write, "w") as csvfile:
        spamwriter = csv.writer(csvfile, delimiter=',')
        for i in xrange(len(fix)): 
            spamwriter.writerow([rel_fit[i], fix[i]])

